package com.zlp.controller;


import com.alibaba.fastjson.JSON;
import com.zlp.dto.CacheInfo;
import com.zlp.dto.LoginUserReq;
import com.zlp.dto.UserQueryReq;
import com.zlp.dto.UserResp;
import com.zlp.entity.User;
import com.zlp.service.UserService;
import com.zlp.common.api.Pager;
import com.zlp.common.api.Result;
import com.zlp.utils.RedisUtils;
import com.zlp.utils.RestTemplateUtil;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 管理用户表 前端控制器
 * </p>
 *
 * @author LiPing.Zou
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(value = "user", tags = "用户模块")
public class UserController {

    private final UserService userService;
    private final RedisUtils redisUtils;


    @Value("${relocationSwitch.whitelist:123}")
    private List<String> whitelist;


    @ApiOperation( "登录")
    @PostMapping(value = "login")
    public Result<CacheInfo> login(@Valid @RequestBody LoginUserReq loginReq){
        System.out.println("whitelist"+JSON.toJSONString(whitelist));
        String memberId = "456";
        if (whitelist.contains(memberId)){
            return Result.success(null);
        }
        return Result.success(userService.login(loginReq));
    }


    @PostMapping("getUserPage")
    @ApiOperation("获取用户列表")
    public Result<Pager<UserResp>> getUserPage(@Valid @RequestBody UserQueryReq userQueryReq){
        return Result.success(userService.getUserPage(userQueryReq));
    }

    @GetMapping("getUserInfo")
    @ApiOperation("获取用户信息")
    public Result<UserResp> getUserInfo(
            @RequestParam(value="userId" )  @ApiParam(name="userId",value="用户ID",required = true) Long userId
    ){

        return Result.success(userService.getUserInfo(userId));
    }

    @PostMapping(value = "/api/invoice/insertOrUpdate")
    @ApiOperation("发票信息")
    public Result insertOrUpdate(
            @ApiParam(value = "订单id") @Param(value = "orderId") String orderId,
            @ApiParam(value = "发票类型(0:个人, 1:企业, 2:专票)") @Param(value = "invoiceType") Integer invoiceType,
            @ApiParam(value = "发票抬头文案") @Param(value = "invoiceTitle") String invoiceTitle,
            @ApiParam(value = "税号") @Param(value = "taxNumber") String taxNumber
    ) {
        System.out.println("orderId= "+orderId);
        System.out.println("invoiceType= "+invoiceType);
        System.out.println("invoiceTitle= "+invoiceTitle);
        System.out.println("taxNumber= "+taxNumber);
        return Result.success(null);
    }

//    curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' http://127.0.0.1:8082/basic/user/api/invoice/insertOrUpdate?orderId=2323\&invoiceType=323\&invoiceTitle=323\&taxNumber=323\


    @ApiOperation( "新增用户")
    @PostMapping(value = "insert")
    public Result<User> insert(@Valid @RequestBody LoginUserReq loginReq){

        return Result.success(userService.insert(loginReq));
    }

    public static void main(String[] args) {
        Integer i = new Integer(2);
        Optional.ofNullable(i).ifPresent(t -> t = 3);
        System.out.println(i);

        int j = 4;
        Optional.ofNullable(i).ifPresent(t -> t = 5);
        System.out.println(i);

//        User user = new User(1L);
//        Optional.ofNullable(i).ifPresent(t -> t = new User(2));

    }

    @ApiOperation( "获取缓存")
    @PostMapping(value = "getRedis")
    public Result<Object> getRedis(){
        String key="key";
        List<String> keys = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            keys.add(key+i);
            redisUtils.set(key+i,i+"");
        }
        keys.add("34");
//        3 存在
        // 2 存在 if (3> 2)
        List<Object> pipelinedResult = redisUtils.getPipelinedResult(keys);
        System.out.println(JSON.toJSONString(pipelinedResult));
        return Result.success(pipelinedResult);
    }


}

